package com.mszlu.blog.common.aop;

import com.mszlu.blog.service.rabbitMQ.RabbitMQServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component
@Aspect //切面  定义了通知和切点的关系
@Slf4j
public class ClearCacheAspect {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private RabbitMQServiceImpl rabbitMQService;

    @Pointcut("@annotation(com.mszlu.blog.common.aop.ClearCache)")
    public void pt(){}

    //环绕通知
    @Around("pt()")
    public Object dos(ProceedingJoinPoint joinPoint) throws Throwable {

        //清空所有缓存
        Set keys = redisTemplate.keys("*");
//        redisTemplate.delete(keys);
        rabbitMQService.delete(keys);

        Object result = joinPoint.proceed();

//        redisTemplate.delete(keys);
        rabbitMQService.delete(keys);

        return result;
    }

}
